/*
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef USERINFO_H
#define USERINFO_H

#include "TypedListViewCtrl.h"
#include "../client/DCPlusPlus.h" //[+]IRainman
#include "../client/ClientManager.h"
#include "../client/UserInfoBase.h"

#include "../client/FastAlloc.h"
#include "../client/TaskQueue.h"

enum Tasks { UPDATE_USER_JOIN, UPDATE_USER, REMOVE_USER, ADD_CHAT_LINE,
             ADD_STATUS_LINE, ADD_SILENT_STATUS_LINE, SET_WINDOW_TITLE, GET_PASSWORD,
             PRIVATE_MESSAGE, STATS, CONNECTED, DISCONNECTED, CHEATING_USER,
             GET_SHUTDOWN, SET_SHUTDOWN, KICK_MSG, DIRECT_MODE_DETECTED
           };

struct UserTask : public Task
{
	UserTask(const UserPtr& u, Client* client_) : user(u), client(client_) { }
	// !SMT!-S
	virtual void beforeExecute()
	{
		if (user)
			if (client && user == ClientManager::getInstance()->getMe())
				identity = client->getMyIdentity();
			else
				ClientManager::getInstance()->getIdentity(user, identity);
	}
	
	UserPtr user;
	Identity identity;
	Client* client; // !SMT!-S: client required for distincting self on different hubs
};

struct MessageTask : public StringTask
{
	MessageTask(const string &strFromUserName_, const UserPtr& from_, const UserPtr& to_, const UserPtr& replyTo_, const string& m, bool thirdPerson_) : StringTask(m), // !SMT!-S
		from(from_), to(to_), replyTo(replyTo_), thirdPerson(thirdPerson_), strFromUserName(strFromUserName_) { }
		
	// !SMT!-S
	virtual void beforeExecute()
	{
		ClientManager::getInstance()->getIdentity(from, fromId);
		ClientManager::getInstance()->getIdentity(replyTo, replyToId);
	}
	
	// [+] brain-ripper
	// in case of own message "from" member contains
	// ClientManager::getInstance()->getMe() identity,
	// which doesn't support per-hub names and always returns
	// default name.
	// So store real per-hub name in strFromUserName member
	string strFromUserName;
	
	UserPtr from;
	UserPtr to;
	UserPtr replyTo;
	bool thirdPerson; // !SMT!-S
	Identity fromId;
	Identity replyToId;
};

enum
{
	COLUMN_FIRST,
	COLUMN_NICK = COLUMN_FIRST,
	COLUMN_SHARED,
	COLUMN_EXACT_SHARED,
	COLUMN_DESCRIPTION,
	COLUMN_TAG,
#ifdef IRAINMAN_INCLUDE_FULL_USER_INFORMATION_ON_HUB
	COLUMN_CONNECTION,
#endif
	COLUMN_EMAIL,
#ifdef IRAINMAN_INCLUDE_FULL_USER_INFORMATION_ON_HUB
	COLUMN_VERSION,
	COLUMN_MODE,
#endif
	COLUMN_HUBS,
	COLUMN_SLOTS,
#ifdef IRAINMAN_INCLUDE_FULL_USER_INFORMATION_ON_HUB
	COLUMN_UPLOAD_SPEED,
#endif
	COLUMN_IP,
	COLUMN_UPLOAD,
	COLUMN_DOWNLOAD,
	COLUMN_LAST_IP,
#ifdef PPA_INCLUDE_DNS
	COLUMN_DNS, // !SMT!-IP
#endif
//[-]PPA        COLUMN_PK,
	COLUMN_CID,
	COLUMN_LAST
};

class UserInfo : public UserInfoBase, public FastAlloc<UserInfo>,
#if defined(PPA_INCLUDE_DNS) && defined(IRAINMAN_INCLUDE_FULL_USER_INFORMATION_ON_HUB)
	public ColumnBase< 18 >
#endif
#if defined(PPA_INCLUDE_DNS) && !defined(IRAINMAN_INCLUDE_FULL_USER_INFORMATION_ON_HUB)
	public ColumnBase< 14 >
#endif
#if !defined(PPA_INCLUDE_DNS) && defined(IRAINMAN_INCLUDE_FULL_USER_INFORMATION_ON_HUB)
	public ColumnBase< 17 >
#endif
#if !defined(PPA_INCLUDE_DNS) && !defined(IRAINMAN_INCLUDE_FULL_USER_INFORMATION_ON_HUB)
	public ColumnBase< 13 >
#endif
{
	public:
		
		UserInfo(const UserTask& u) : user(u.user), m_FlagImage(-1)
		{
			update(u.identity, -1);
		}
		static int compareItems(const UserInfo* a, const UserInfo* b, int col);
		uint8_t getImageIndex() const
		{
			return UserInfoBase::getImage(identity, 0);
		}
		
		bool update(const Identity& identity, int sortCol);
		
		const string getNick() const
		{
			return identity.getNick();
		}
		bool isHidden() const
		{
			return identity.isHidden();
		}
		
		tstring getText(int col) const;
		
		const UserPtr& getUser() const
		{
			return user;
		}
		
		GETSET(Identity, identity, Identity);
		int m_FlagImage;
		
		// !SMT!-S
		typedef unordered_map<UserPtr, UserInfo*, User::Hash> UserMapBase;
		typedef UserMapBase::const_iterator UserMapIter;
		class UserMap : public UserMapBase
		{
			public:
				UserInfo* findUser(const UserPtr& user)
				{
					UserMapIter i = find(user);
					return (i == end()) ? NULL : i->second;
				}
		};
		
		UserPtr user;
};


#endif //USERINFO_H
